# Face++请求范例
import os
import re
import base64
import requests
import time
path = '/Users/jiangce/Desktop/images/'
jud_file = re.compile(r'\.')#.需要通过转置

def postImage(path):
    f = open(path, 'rb')
    image_base64 = base64.b64encode(f.read())
    payload = {
        'api_key': 'Av8BtIbHp2CIN3-mEghiTYZYuM2pIMb0',
        'api_secret': 'uzNKzzqFwLBtBjpAYM7rdym0fWkHQ3Jt',
        'image_base64': image_base64
    }
    r = requests.post(r'https://api-cn.faceplusplus.com/imagepp/v1/licenseplate',data = payload)
    #print(r.json()["results"][0]["license_plate_number"])对于requests的返回要先进行解码(json)然后根据返回的内容解码
    results = r.json()["results"]
    time.sleep(10)
    if len(results) == 0:
        return path
    else:
        return path + "\t" + results[0]["license_plate_number"]

#postImage('/Users/jiangce/Desktop/1657-苏JF6533-黄牌.jpg')#测试用
if __name__ == '__main__':
    file_list = os.listdir(path)
    num = 1
    with open('/Users/jiangce/Desktop/test.txt','w') as f:#'w'重新写代码
        for file in file_list:
            if not jud_file.match(file):#剔除隐藏文件
                path_file = os.path.join(path,file)
                images = os.listdir(path_file)
                for image in images:
                    path_image = os.path.join(path_file,image)#合并两个路径用这个方法合并，直接相加容易拉掉/
                    f.write(postImage(path_image))
                    f.write('\n')
                    print("num is {}".format(num))
                    num+=1


